<?php

class Lotes_M extends I_Model {

    protected $_table_name = 'lotes';
    protected $_primary_key = 'ID';
    protected $_primary_filter = 'intval';
    protected $_order_by = 'NUM_LOTE';
    public $rules = array();
    protected $_timestamps = FALSE;

    public function __construct() {
        parent::__construct();
    }

    public function save($data, $id = null, $batch = false) {
        if ($batch) {
            $this->db->insert_batch($this->_table_name, $data);
            if (!$this->db->insert_id()) {
                $this->log_errors();
                return false;
            }
            return true;
        }

        return parent::save($data, $id);
    }

    public function fullInfo($id) {
        $db_query = $this->db->select('lt.*,le.DATA,cat.ID as CATEGORIE_ID, tip.ID as TIPO_ATIVO, city.ID as CIDADE_ID, uf.ID as ESTADO')
                        ->from('lotes as lt')
                        ->join('tipos_ativos as tip', 'tip.ID=lt.TIPO_ID', 'LEFT')
                        ->join('categorias as cat', 'cat.ID=tip.CATEGORIA_ID', 'LEFT')
                        ->join('leiloes as le', 'le.ID=lt.LEILAO_ID')
                        ->join('cidades as city', 'city.ID=lt.CIDADE', 'LEFT')
                        ->join('estados as uf', 'uf.ID=city.ESTADO', 'LEFT')->where(array(
                    'lt.ID' => $id
                ))->get();

        return $db_query->row();
    }

    public function get_all_by_leilao($id = null, $slug = null, $uf = null, $limit = 0, $offset = 0) {

        if (!is_null($id)) {
            $this->db->where(array('le.ID' => $id));
        }
        if (!is_null($slug)) {
            $this->db->where(array('cat.SLUG' => $slug));
            $this->db->where('(le.STATUS = 1 OR le.STATUS=2)');
        }
        if (!is_null($uf)) {
            $this->db->where(array('uf.UF' => $uf));
            $this->db->where('(le.STATUS = 1 OR le.STATUS=2)');
        }

        if ($limit > 0):
            $this->db->limit($limit, $offset);
        endif;
        $rs = $this->db->select('le.*,cl.*,uf.UF,
				ci.NOME as LOCALIZACAO ,
				vst.COUNT as VISITAS,
				lc.COMPRADORES,
				lc.LANCES,
				lc2.LAST_LANCE,
				lc2.USER_ID,
				
				lc.TIPO AS LANCE_TIPO,
				tp.SLUG as SLUG_TIPO,cat.SLUG, cat.NAME as CATEGORIA,tp.NAME as TIPO_ATIVO,
				lcp.TEMPO_EXTRA,
				lcc.COMPRADORES_LEILAO,
				lcc.LANCES_LEILAO,
				hbb.HABILITADOS,
				ltt.TOTAL_LOTES,
				lc2.LOGIN as GANHADOR,
				lcpu.VENDIDO,lcpu.CONDICIONAL,lcpu.FECHADO,
				lt.*')
                ->from('lotes as lt')
                ->join('tipos_ativos as tp', 'lt.TIPO_ID = tp.ID', 'left')
                ->join('lances_arrematados as lcpu', 'lcpu.LOTE_ID=lt.ID', 'left')
                ->join('categorias as cat', 'cat.ID = tp.CATEGORIA_ID')
                ->join('leiloes as le', 'le.ID = lt.LEILAO_ID')
                ->join('cidades as ci', 'ci.ID = lt.CIDADE')
                ->join('visitas_lotes as vst', 'vst.LOTE_ID=lt.ID', 'LEFT')
                ->join('( SELECT LOTE_ID,COUNT(ID) AS LANCES,TIPO,COUNT(DISTINCT USER_ID) AS COMPRADORES FROM lances GROUP BY LOTE_ID ) as lc', 'lc.LOTE_ID=lt.ID', 'LEFT')
                ->join('( SELECT ID AS IDD,LOTE_ID,TIPO, TOTAL AS LAST_LANCE,LOGIN,USER_ID  FROM (SELECT * FROM lances ORDER BY ID DESC ) as  lin GROUP BY LOTE_ID ) as lc2', 'lc2.LOTE_ID=lt.ID', 'LEFT')
                ->join('( SELECT LEILAO_ID,COUNT(DISTINCT ID) LANCES_LEILAO, COUNT(DISTINCT USER_ID) AS COMPRADORES_LEILAO  FROM lances GROUP BY LEILAO_ID ) as lcc', 'lcc.LEILAO_ID=le.ID', 'LEFT')
                ->join('( SELECT ID AS IDD,LOTE_ID, TEMPO AS TEMPO_EXTRA  FROM lances_tempo_extra GROUP BY LOTE_ID ORDER BY IDD DESC ) as lcp', 'lcp.LOTE_ID=lt.ID', 'LEFT')
                ->join('( SELECT LEILAO_ID,COUNT(DISTINCT ID) AS HABILITADOS  FROM habilitacoes WHERE BLOCKED = "N" GROUP BY LEILAO_ID  ) as hbb', 'hbb.LEILAO_ID=le.ID', 'LEFT')
                ->join('( SELECT LEILAO_ID,COUNT(DISTINCT ID) AS  TOTAL_LOTES FROM lotes GROUP BY LEILAO_ID  ) as ltt', 'ltt.LEILAO_ID=le.ID', 'LEFT')
                ->join('estados as uf', 'uf.ID = ci.ESTADO')
                ->join('comitentes as cl', 'cl.ID=le.COMITENTE_ID')
                //->join('users as us', 'us.ID=lc.USER_ID', 'left')
                ->order_by('lt.CLOSED,lt.FECHAMENTO,lt.NUM_LOTE', 'ASC')
                ->group_by('lt.ID')
                ->get();




        return $rs->result();
    }

    public function get_all_by_leilao_closed($id = null, $slug = null, $uf = null, $limit = 0, $offset = 0) {

        if (!is_null($id)) {
            $this->db->where(array('le.ID' => $id));
            $this->db->where('(le.STATUS = 4)');
        }
        if (!is_null($slug)) {
            $this->db->where(array('cat.SLUG' => $slug));
            $this->db->where('(le.STATUS = 4)');
        }
        if (!is_null($uf)) {
            $this->db->where(array('uf.UF' => $uf));
            $this->db->where('(le.STATUS = 4)');
        }

        if ($limit > 0):
            $this->db->limit($limit, $offset);
        endif;
        $rs = $this->db->select('le.*,cl.*,uf.UF,
				ci.NOME as LOCALIZACAO ,
				vst.COUNT as VISITAS,
				lc.COMPRADORES,
				lc.LANCES,
				lc2.LAST_LANCE,
				lc2.USER_ID,
				
				lc.TIPO AS LANCE_TIPO,
				tp.SLUG as SLUG_TIPO,cat.SLUG, cat.NAME as CATEGORIA,tp.NAME as TIPO_ATIVO,
				lcp.TEMPO_EXTRA,
				lcc.COMPRADORES_LEILAO,
				lcc.LANCES_LEILAO,
				hbb.HABILITADOS,
				ltt.TOTAL_LOTES,
				lc2.LOGIN as GANHADOR,
				lcpu.VENDIDO,lcpu.CONDICIONAL,lcpu.FECHADO,
				lt.*')
                ->from('lotes as lt')
                ->join('tipos_ativos as tp', 'lt.TIPO_ID = tp.ID', 'left')
                ->join('lances_arrematados as lcpu', 'lcpu.LOTE_ID=lt.ID', 'left')
                ->join('categorias as cat', 'cat.ID = tp.CATEGORIA_ID')
                ->join('leiloes as le', 'le.ID = lt.LEILAO_ID')
                ->join('cidades as ci', 'ci.ID = lt.CIDADE')
                ->join('visitas_lotes as vst', 'vst.LOTE_ID=lt.ID', 'LEFT')
                ->join('( SELECT LOTE_ID,COUNT(ID) AS LANCES,TIPO,COUNT(DISTINCT USER_ID) AS COMPRADORES FROM lances GROUP BY LOTE_ID ) as lc', 'lc.LOTE_ID=lt.ID', 'LEFT')
                ->join('( SELECT ID AS IDD,LOTE_ID,TIPO, TOTAL AS LAST_LANCE,LOGIN,USER_ID  FROM (SELECT * FROM lances ORDER BY ID DESC ) as  lin GROUP BY LOTE_ID ) as lc2', 'lc2.LOTE_ID=lt.ID', 'LEFT')
                ->join('( SELECT LEILAO_ID,COUNT(DISTINCT ID) LANCES_LEILAO, COUNT(DISTINCT USER_ID) AS COMPRADORES_LEILAO  FROM lances GROUP BY LEILAO_ID ) as lcc', 'lcc.LEILAO_ID=le.ID', 'LEFT')
                ->join('( SELECT ID AS IDD,LOTE_ID, TEMPO AS TEMPO_EXTRA  FROM lances_tempo_extra GROUP BY LOTE_ID ORDER BY IDD DESC ) as lcp', 'lcp.LOTE_ID=lt.ID', 'LEFT')
                ->join('( SELECT LEILAO_ID,COUNT(DISTINCT ID) AS HABILITADOS  FROM habilitacoes WHERE BLOCKED = "N" GROUP BY LEILAO_ID  ) as hbb', 'hbb.LEILAO_ID=le.ID', 'LEFT')
                ->join('( SELECT LEILAO_ID,COUNT(DISTINCT ID) AS  TOTAL_LOTES FROM lotes GROUP BY LEILAO_ID  ) as ltt', 'ltt.LEILAO_ID=le.ID', 'LEFT')
                ->join('estados as uf', 'uf.ID = ci.ESTADO')
                ->join('comitentes as cl', 'cl.ID=le.COMITENTE_ID')
                //->join('users as us', 'us.ID=lc.USER_ID', 'left')
                ->order_by('lt.FECHAMENTO,lt.NUM_LOTE', 'ASC')
                ->group_by('lt.ID')
                ->get();


        return $rs->result();
    }

    public function get_all_by_leilao_promo($id = null, $slug = null, $uf = null, $limit = 0, $offset = 0) {

        if (!is_null($id)) {
            $this->db->where(array('le.ID' => $id));
        }
        if (!is_null($slug)) {
            $this->db->where(array('cat.SLUG' => $slug));
            $this->db->where('(le.STATUS = 1 OR le.STATUS=2)');
        }
        if (!is_null($uf)) {
            $this->db->where(array('uf.UF' => $uf));
            $this->db->where('(le.STATUS = 1 OR le.STATUS=2)');
        }

        if ($limit > 0):
            $this->db->limit($limit, $offset);
        endif;
        $this->db->where(array('le.TIPO' => 'PO'));
        $rs = $this->db->select('le.*,cl.*,uf.UF,
				ci.NOME as LOCALIZACAO ,
				vst.COUNT as VISITAS,
				lc.COMPRADORES,
				lc.LANCES,
				lc2.LAST_LANCE,
				lc2.USER_ID,
				
				lc.TIPO AS LANCE_TIPO,
				tp.SLUG as SLUG_TIPO,cat.SLUG, cat.NAME as CATEGORIA,tp.NAME as TIPO_ATIVO,
				lcp.TEMPO_EXTRA,
				lcc.COMPRADORES_LEILAO,
				lcc.LANCES_LEILAO,
				hbb.HABILITADOS,
				ltt.TOTAL_LOTES,
				lc2.LOGIN as GANHADOR,
				lcpu.VENDIDO,lcpu.CONDICIONAL,lcpu.FECHADO,
				lt.*')
                ->from('lotes as lt')
                ->join('tipos_ativos as tp', 'lt.TIPO_ID = tp.ID', 'left')
                ->join('lances_arrematados as lcpu', 'lcpu.LOTE_ID=lt.ID', 'left')
                ->join('categorias as cat', 'cat.ID = tp.CATEGORIA_ID')
                ->join('leiloes as le', 'le.ID = lt.LEILAO_ID')
                ->join('cidades as ci', 'ci.ID = lt.CIDADE')
                ->join('visitas_lotes as vst', 'vst.LOTE_ID=lt.ID', 'LEFT')
                ->join('( SELECT LOTE_ID,COUNT(ID) AS LANCES,TIPO,COUNT(DISTINCT USER_ID) AS COMPRADORES FROM lances GROUP BY LOTE_ID ) as lc', 'lc.LOTE_ID=lt.ID', 'LEFT')
                ->join('( SELECT ID AS IDD,LOTE_ID,TIPO, TOTAL AS LAST_LANCE,LOGIN,USER_ID  FROM (SELECT * FROM lances ORDER BY ID DESC ) as  lin GROUP BY LOTE_ID ) as lc2', 'lc2.LOTE_ID=lt.ID', 'LEFT')
                ->join('( SELECT LEILAO_ID,COUNT(DISTINCT ID) LANCES_LEILAO, COUNT(DISTINCT USER_ID) AS COMPRADORES_LEILAO  FROM lances GROUP BY LEILAO_ID ) as lcc', 'lcc.LEILAO_ID=le.ID', 'LEFT')
                ->join('( SELECT ID AS IDD,LOTE_ID, TEMPO AS TEMPO_EXTRA  FROM lances_tempo_extra GROUP BY LOTE_ID ORDER BY IDD DESC ) as lcp', 'lcp.LOTE_ID=lt.ID', 'LEFT')
                ->join('( SELECT LEILAO_ID,COUNT(DISTINCT ID) AS HABILITADOS  FROM habilitacoes WHERE BLOCKED = "N" GROUP BY LEILAO_ID  ) as hbb', 'hbb.LEILAO_ID=le.ID', 'LEFT')
                ->join('( SELECT LEILAO_ID,COUNT(DISTINCT ID) AS  TOTAL_LOTES FROM lotes GROUP BY LEILAO_ID  ) as ltt', 'ltt.LEILAO_ID=le.ID', 'LEFT')
                ->join('estados as uf', 'uf.ID = ci.ESTADO')
                ->join('comitentes as cl', 'cl.ID=le.COMITENTE_ID')
                //->join('users as us', 'us.ID=lc.USER_ID', 'left')
                ->order_by('lt.NUM_LOTE', 'ASC')
                ->group_by('lt.ID')
                ->get();




        return $rs->result();
    }

    public function delete_all_leilao($id) {

        $this->db->delete($this->_table_name, array('LEILAO_ID' => $id));
    }

    public function get_in($str) {


        $this->db->where_in('lt.ID',$str);
        
//        $sql = " SELECT * FROM (SELECT * FROM lances ORDER BY ID DESC ) as t WHERE LOTE_ID IN('2683','2687') GROUP BY LOTE_ID";
//        return  $this->db->query($sql)->result();
        
        
        $rs = $this->db->select('le.*,cl.*,uf.UF,
				ci.NOME as LOCALIZACAO ,
				vst.COUNT as VISITAS,
				lc.COMPRADORES,
				lc.LANCES,
				lc2.USER_ID,
				lc2.TIPO,
				lc2.LAST_LANCE,
				tp.SLUG as SLUG_TIPO,cat.SLUG, cat.NAME as CATEGORIA,tp.NAME as TIPO_ATIVO,
				lcp.TEMPO_EXTRA,
				lcc.COMPRADORES_LEILAO,
				lcc.LANCES_LEILAO,
				hbb.HABILITADOS,
				ltt.TOTAL_LOTES,
				lc2.LOGIN as GANHADOR,
				lcpu.VENDIDO,lcpu.CONDICIONAL,
				lt.*')
                ->from('lotes as lt')
                ->join('tipos_ativos as tp', 'lt.TIPO_ID = tp.ID', 'left')
                ->join('lances_arrematados as lcpu', 'lcpu.LOTE_ID=lt.ID', 'left')
                ->join('categorias as cat', 'cat.ID = tp.CATEGORIA_ID')
                ->join('leiloes as le', 'le.ID = lt.LEILAO_ID')
                ->join('cidades as ci', 'ci.ID = lt.CIDADE', 'left')
                ->join('visitas_lotes as vst', 'vst.LOTE_ID=lt.ID', 'LEFT')
                ->join('( SELECT LOTE_ID,COUNT(ID) AS LANCES,COUNT(DISTINCT USER_ID) AS COMPRADORES FROM lances GROUP BY LOTE_ID ) as lc', 'lc.LOTE_ID=lt.ID', 'LEFT')
                ->join('( SELECT ID AS IDD,LOTE_ID,TIPO, TOTAL AS LAST_LANCE,LOGIN,USER_ID  FROM (SELECT * FROM lances ORDER BY ID DESC ) as  lin GROUP BY LOTE_ID ) as lc2', 'lc2.LOTE_ID=lt.ID', 'LEFT')
                ->join('( SELECT LEILAO_ID,COUNT(DISTINCT ID) LANCES_LEILAO, COUNT(DISTINCT USER_ID) AS COMPRADORES_LEILAO  FROM lances GROUP BY LEILAO_ID ) as lcc', 'lcc.LEILAO_ID=le.ID', 'LEFT')
                ->join('( SELECT ID AS IDD,LOTE_ID, TEMPO AS TEMPO_EXTRA  FROM lances_tempo_extra GROUP BY LOTE_ID ORDER BY IDD DESC ) as lcp', 'lcp.LOTE_ID=lt.ID', 'LEFT')
                ->join('( SELECT LEILAO_ID,COUNT(DISTINCT ID) AS HABILITADOS  FROM habilitacoes WHERE BLOCKED = "N" GROUP BY LEILAO_ID  ) as hbb', 'hbb.LEILAO_ID=le.ID', 'LEFT')
                ->join('( SELECT LEILAO_ID,COUNT(DISTINCT ID) AS  TOTAL_LOTES FROM lotes GROUP BY LEILAO_ID  ) as ltt', 'ltt.LEILAO_ID=le.ID', 'LEFT')
                ->join('estados as uf', 'uf.ID = ci.ESTADO', 'left')
                ->join('comitentes as cl', 'cl.ID=le.COMITENTE_ID', 'left')
                //->join('users as us', 'us.ID=lc.USER_ID', 'left')
                ->order_by('lt.FECHAMENTO,lt.NUM_LOTE', 'ASC')
                ->group_by('lt.ID')
                ->get();

              
        return $rs->result();
    }

    public function lotes_detail($id) {
        $this->db->select('lt.ID as CODLOTE,
				    lt.DESCRICAO,lt.VALOR_MINIMO,lt.LANCE_INICIAL,lt.INCREMENTO,lt.COMISSAO,
				     lt.RETIRADO,lt.LINK,lt.IMAGENS,lt.FECHAMENTO,lt.NUM_LOTE,
				     lt.SUB_DESCRICAO,lt.COMITENTE_ID,lt.DESCRICAO_DETALHADA,lt.LINK,lt.INFO,
                                     lt.INFO_RETIRADA,lt.INFO_VISITACAO,
				    lc4.LANCES,
				    lc3.LAST_LANCE,
				    lc3.ID as LANCE_ID,
				    lc3.LANCE_TIPO,
				   tp.NAME as TIPO_ATIVO,cl.NOME_FANTASIA as CLIENTE,cl.LOGO,lc2.COMPRADORES,'
                        . 'le.ID as LEILAO_ID ,le.DATA,le.TIPO,lc3.LOGIN,le.CRIADO AS ABERTURA,le.IMAGEM,le.DESCRICAO,'
                        . 'le.STATUS,cat.SLUG,cat.NAME as CATEGORIA,uf.UF ,cid.NOME as CIDADE_LOTE,vst.COUNT as VISITAS,
				lcp.VENDIDO,lcp.CONDICIONAL,lcp.FECHADO')
                ->from('lotes as lt')
                ->join('visitas_lotes as vst', 'vst.LOTE_ID = lt.ID', 'left')
                ->join('tipos_ativos as tp', 'tp.ID=lt.TIPO_ID', 'left')
                ->join('lances_arrematados as lcp', 'lcp.LOTE_ID=lt.ID', 'left')
                //->join('lances as lc', 'lc.LOTE_ID=lt.ID', 'left')
                ->join('(SELECT ID AS IDC,LOTE_ID,COUNT(DISTINCT USER_ID) AS COMPRADORES FROM lances  GROUP BY LOTE_ID ) as lc2', 'lc2.LOTE_ID=lt.ID', 'left')
                ->join('(SELECT ID ,LOTE_ID,LOGIN,TOTAL AS LAST_LANCE,TIPO AS LANCE_TIPO  FROM lances ORDER BY ID ASC ) as lc3', 'lc3.LOTE_ID=lt.ID', 'LEFT')
                ->join('(SELECT LOTE_ID,COUNT(ID) AS LANCES FROM lances GROUP BY LOTE_ID ) as lc4', 'lc4.LOTE_ID=lt.ID', 'left')
                //->join('users as us', 'us.ID=lc.USER_ID', 'left')
                ->join('categorias as cat', 'cat.ID=tp.CATEGORIA_ID', 'left')
                ->join('cidades as cid', 'cid.ID=lt.CIDADE', 'left')
                ->join('estados as uf', 'cid.ESTADO=uf.ID', 'left')
                ->join('leiloes as le', 'le.ID=lt.LEILAO_ID')
                ->join('comitentes as cl', 'cl.ID = le.COMITENTE_ID', 'left')
                ->where(array('le.ID' => $id))
                ->order_by('lt.FECHAMENTO,lt.NUM_LOTE', 'ASC');

        $q = $this->db->get()->result();
        $data = new stdClass();
        foreach ($q as $row) {
            $lote = $row->CODLOTE;
            $data->$lote = $row;
        }



        return $data;
    }

    function last_num($id) {
        $sql = "SELECT NUM_LOTE AS NUM_LOTE FROM lotes WHERE LEILAO_ID={$id} ORDER BY ID DESC LIMIT 1 ";

        $q = $this->db->query($sql);
        if ($q->num_rows() == 0) {
            return str_num_lote_pad(1);
        }
        return str_num_lote_pad($q->row()->NUM_LOTE + 1);
    }

    public function get_all_by_search($search) {

        $rs = $this->db->select('le.*,cl.*,uf.UF,
				ci.NOME as LOCALIZACAO ,
				vst.COUNT as VISITAS,
				lc.COMPRADORES,
				lc.LANCES,
				lc2.LAST_LANCE,
				tp.SLUG as SLUG_TIPO,cat.SLUG, cat.NAME as CATEGORIA,tp.NAME as TIPO_ATIVO,
				lcp.LANCE_PLUS,
				lcc.COMPRADORES_LEILAO,
				lcc.LANCES_LEILAO,
				hbb.HABILITADOS,
				ltt.TOTAL_LOTES,
				lc2.LOGIN as GANHADOR,
				lcpu.VENDIDO,lcpu.CONDICIONAL,
				lt.*')
                ->from('lotes as lt')
                ->join('tipos_ativos as tp', 'lt.TIPO_ID = tp.ID', 'left')
                ->join('lances_arrematados as lcpu', 'lcpu.LOTE_ID=lt.ID', 'left')
                ->join('categorias as cat', 'cat.ID = tp.CATEGORIA_ID')
                ->join('leiloes as le', 'le.ID = lt.LEILAO_ID')
                ->join('cidades as ci', 'ci.ID = lt.CIDADE', 'left')
                ->join('visitas_lotes as vst', 'vst.LOTE_ID=lt.ID', 'LEFT')
                ->join('( SELECT LOTE_ID,COUNT(ID) AS LANCES,COUNT(DISTINCT USER_ID) AS COMPRADORES FROM lances GROUP BY LOTE_ID ) as lc', 'lc.LOTE_ID=lt.ID', 'LEFT')
                ->join('( SELECT ID AS IDD,LOTE_ID, TOTAL AS LAST_LANCE,LOGIN  FROM lances ORDER BY IDD DESC ) as lc2', 'lc2.LOTE_ID=lt.ID', 'LEFT')
                ->join('( SELECT LEILAO_ID,COUNT(DISTINCT ID) LANCES_LEILAO, COUNT(DISTINCT USER_ID) AS COMPRADORES_LEILAO  FROM lances GROUP BY LEILAO_ID ) as lcc', 'lcc.LEILAO_ID=le.ID', 'LEFT')
                ->join('( SELECT ID AS IDD,LOTE_ID, DATA AS LANCE_PLUS  FROM lotes_plus_time GROUP BY LOTE_ID ORDER BY IDD DESC ) as lcp', 'lcp.LOTE_ID=lt.ID', 'LEFT')
                ->join('( SELECT LEILAO_ID,COUNT(DISTINCT ID) AS HABILITADOS  FROM habilitacoes WHERE BLOCKED = "N" GROUP BY LEILAO_ID  ) as hbb', 'hbb.LEILAO_ID=le.ID', 'LEFT')
                ->join('( SELECT LEILAO_ID,COUNT(DISTINCT ID) AS  TOTAL_LOTES FROM lotes GROUP BY LEILAO_ID  ) as ltt', 'ltt.LEILAO_ID=le.ID', 'LEFT')
                ->join('estados as uf', 'uf.ID = ci.ESTADO', 'left')
                ->join('comitentes as cl', 'cl.ID=le.COMITENTE_ID', 'left')
                //->join('users as us', 'us.ID=lc.USER_ID', 'left')
                ->order_by('lt.FECHAMENTO,lt.NUM_LOTE', 'ASC')
                ->like('lt.DESCRICAO', $search)
                ->or_like('lt.TAGS', $search)
                ->or_like('lt.SUB_DESCRICAO', $search)
                ->or_like('lt.DESCRICAO_DETALHADA', $search)
                ->where('le.STATUS <', 3)
                ->group_by('lt.ID')
                ->get();


        return $rs->result();
    }

    function not_seller($id) {
        $sql = "SELECT lt.* FROM lotes as lt
WHERE lt.LEILAO_ID='{$id}' AND ID NOT IN (SELECT LOTE_ID FROM lances_arrematados)	
ORDER BY NUM_LOTE ASC";
        $q = $this->db->query($sql);
        return $q->result_array();
    }

    function save_batch($data) {
        $this->db->trans_start();
        $this->db->insert_batch($this->_table_name, $data);
        $this->db->trans_complete();
        return $this->db->trans_status();
    }

    function update_batch($data) {
        $this->db->trans_start();
        $this->db->update_batch($this->_table_name, $data, $this->_primary_key);
        $this->db->trans_complete();
        return $this->db->trans_status();
    }

    function num_lote_by_leilao($id) {
        $sql = "SELECT MAX(NUM_LOTE) as NUM_LOTE FROM lotes WHERE LEILAO_ID='{$id}'";

        return $this->db->query($sql)->row()->NUM_LOTE;
    }

}
